package com.qfedu.hotelssm.service.impl;


import com.qfedu.hotelssm.dao.RoomsDao;
import com.qfedu.hotelssm.dto.RoomsStatusDto;
import com.qfedu.hotelssm.dto.RoomsStatusTjDto;
import com.qfedu.hotelssm.entity.Rooms;
import com.qfedu.hotelssm.service.intf.RoomsService;
import com.qfedu.hotelssm.util.StringUtil;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mk
 * @date 2022/8/28  21:03
 */
@Service
public class RoomsServiceImpl implements RoomsService {
    @Autowired
    private RoomsDao dao;


    @Override
    public R update(Rooms rooms) {
        if(dao.update(rooms)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageBean selectPage(String name, int page, int limit) {
        if(StringUtil.isNotEmpty(name)){
            name="%"+name+"%";
        }
        int start=(page-1)*limit;
        return new PageBean((int) dao.selectCount(name),dao.selectPage(name,start,limit));
    }

    @Override
    public R roomstatusTj() {

        //1.查询数据
        List<RoomsStatusDto> list=dao.selectStatus();
        List<RoomsStatusTjDto> result=new ArrayList<>();
        //2.组装数据  JDk8新特性
        list.forEach(s->{
            result.add(new RoomsStatusTjDto(parseStatus(s.getRoomstatus()),s.getNum()));
        });
        //3.返回数据
        return R.ok(result);

        }
    private String parseStatus(int status) {
        String r = "";
        switch (status) {

            case 0:
                r = "未入住";
                break;
            case 1:
                r = "已入住";
                break;

        }
        return r;
    }

}
